﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using BLL;
using Model;
using System.Data;
namespace Presentation
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                bindTeacherName();
                bindCourseLevel();
                StudentManager students = new StudentManager();
                this.StudentListBox.DataSource = students.getStudentList(courseID);
                this.StudentListBox.DataTextField = "StudentName";
                this.StudentListBox.DataValueField = "StudentId";
                this.StudentListBox.DataBind();
                this.SelectedListBox.DataSource = students.getSelectedList(courseID);
                this.SelectedListBox.DataTextField = "StudentName";
                this.SelectedListBox.DataValueField = "StudentId";
                this.SelectedListBox.DataBind();
                CourseManager course = new CourseManager();
                if (courseID > 0)
                {
                    List<Model.Course> Course = course.obtainCourseName(courseID);
                    this.CourseNameTextBox.Text = Course.First().CourseName;
                }

            }

        }

        /// <summary>
        /// define CourseId
        /// </summary>
        private int courseID = 16;

        private void bindCourseLevel()
        {
            CourseLevelManager level = new CourseLevelManager();
            this.CourseLevelDropDownList.DataSource = level.getLevelList();
            this.CourseLevelDropDownList.DataTextField = "LevelName";
            this.CourseLevelDropDownList.DataValueField = "LevelId";
            this.CourseLevelDropDownList.DataBind();
            this.CourseLevelDropDownList.Items.Insert(0, "Please Select");
            this.CourseLevelDropDownList.SelectedIndex = 0;
        }
        private void bindTeacherName()
        {
            TeacherManager teacher = new TeacherManager();
            this.TeacherDropDownList.DataSource = teacher.getTeacherList();
            this.TeacherDropDownList.DataTextField = "TeacherName";
            this.TeacherDropDownList.DataValueField = "TeacherId";
            this.TeacherDropDownList.DataBind();
            this.TeacherDropDownList.Items.Insert(0, "Please Select");
            this.TeacherDropDownList.SelectedIndex = 0;
        }

        protected void add_Click(object sender, EventArgs e)
        {
            if (this.StudentListBox != null && this.StudentListBox.SelectedItem != null)
            {
                this.SelectedListBox.Items.Add(this.StudentListBox.SelectedItem);
                this.StudentListBox.Items.Remove(this.SelectedListBox.SelectedItem);
                this.SelectedListBox.SelectedItem.Selected = false;
            }
        }

        protected void Remove_Click(object sender, EventArgs e)
        {
            if (this.SelectedListBox != null && this.SelectedListBox.SelectedItem != null)
            {
                this.StudentListBox.Items.Add(this.SelectedListBox.SelectedItem);
                this.SelectedListBox.Items.Remove(this.StudentListBox.SelectedItem);
                this.StudentListBox.SelectedItem.Selected = false;
            }
        }
        /// <summary>
        /// save operation  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SaveCourse_Click(object sender, EventArgs e)
        {
            if (courseID < 0)
            {
                SavePage(courseID);

            }

            else
            {
                SaveManager course = new SaveManager();
                course.removeCourseDetails(courseID);
                SavePage(courseID);

            }
        }
        public void SavePage(int courseID)
        {
            if (CourseLevelDropDownList.SelectedIndex > 0 && TeacherDropDownList.SelectedIndex > 0)
            {
                int courseId;
                SaveManager BllCourse = new SaveManager();
                Course course = new Course();
                course.CourseName = CourseNameTextBox.Text.Trim();
                course.TeacherId = int.Parse(TeacherDropDownList.SelectedValue.ToString());
                course.LevelId = int.Parse(CourseLevelDropDownList.SelectedValue.ToString());
                course.CourseStatus = 1;
                if (courseID < 0)
                {
                    courseId = BllCourse.addCourse(course);
                }
                else
                {
                    course.CourseId = courseID;
                    BllCourse.updateCourse(course);
                    courseId = courseID;
                }  
                for (int i = 0; i < SelectedListBox.Items.Count; i++)
                {
                    CourseDetails courseDetail = new CourseDetails();
                    courseDetail.CourseId = courseId;
                    courseDetail.StudentId = int.Parse(SelectedListBox.Items[i].Value.ToString());
                    BllCourse.addCourseDetails(courseDetail);
                }
                if (courseID < 0)
                {
                    Response.Write("save successfully");
                }
                else
                {
                    Response.Write("update successfully");
                }
            }
            else
            {

                Response.Redirect("SaveCourse_UI.aspx");

            }
        }
    }
}



